﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Web.UI.WebControls;
using ArtClub.DBAL;
using System.Transactions;

namespace ArtClub.Controller
{
    public class AuctionCtr
    {
        private DataDiagramDataContext data = new DataDiagramDataContext();
        public Auction addAuction(int pieceId, DateTime startTime, double endTime, int bStep, double minBidPrice)
        {
            Auction a = new Auction()
            {
                
                pId = pieceId,
                startTime = startTime,
                endTime = (long)endTime,
                biddingStep = bStep,
                minBiddingPrice = (float)minBidPrice,
                highestBid = (float)minBidPrice
               
            };
            data.Auctions.InsertOnSubmit(a);
            data.SubmitChanges();
            return a;
        }
        public void deleteAuction(int id)
        {
            Auction a = data.Auctions.Where(c => c.id == id).Single();
            data.Auctions.DeleteOnSubmit(a);
            data.SubmitChanges();
        }


        public Auction getAuction(int id)
        {
            
            Auction p = data.Auctions.Where(c => c.id == id).First();
            return p;
        }

        public Auction[] getAllAuctions()
        {
            var table = data.GetTable<Auction>();
            return table.ToArray();
        }

        public Auction[] getCurrentAuctions()
        {
            DateTime Jan1St1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            var table = data.Auctions.Where(c => (DateTime.Now - Jan1St1970).TotalMilliseconds < c.endTime && DateTime.Now>c.startTime);
            return table.ToArray();
        }

        public Auction[] getPastAuctions()
        {
            DateTime Jan1St1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            var table = data.Auctions.Where(c => c.endTime < (DateTime.Now - Jan1St1970).TotalMilliseconds && c.Member != null);
            return table.ToArray();
        }

        public void bidPiece(int auctionId, double bidPrice, int memberId)
        {
            
            using (TransactionScope scope = new TransactionScope())
            {
                Auction a = getAuction(auctionId);
                DateTime Jan1St1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                MemberCtr mCtr = new MemberCtr();
                double point = mCtr.getPoints(memberId);
                if ((DateTime.Now - Jan1St1970).TotalMilliseconds > a.endTime)
                {
                    throw new Exception("Fail to bid, because bidding time is over for the piece.");
                }
                if (bidPrice > point * 3)
                {
                    throw new Exception("Fail to bid, not enough points.");
                }
                if (bidPrice > a.highestBid.Value)
                {
                    a.highestBid = (float)bidPrice;
                    //MemberCtr mCtr = new MemberCtr();
                    //Member bidder = mCtr.getMember(memberId);
                    a.winner = memberId;
                    
                }
                else
                {
                    throw new Exception("You are out bidded.");   
                }
                data.SubmitChanges();
                scope.Complete();
                
            }
          
            
        }
    }
}